#pragma once
#include "d3dutil.h"
#include "LevelElement.h"
#include "Vertex.h"
#include <vector>
#include "InputState.h"

class NxActor;

class Water : public LevelElement
{
public:
	/**
	* Creates a new Water from a file name.
	* @param fileName the location of the Water.
	* @param nrOfColumns the number of columns in the Water.
	* @param nrOfRows the number of rows in the Water.
	* @param tileSize the scale factor for the tiles.
	* @param scaleHeight the scale factor for the height.
	* @param offset a value to add to the height.
	*/

	struct InitInfo
	{
		tstring Bumpmaptexture;
		D3DXVECTOR3 Position;
		float Width;
		float Length;
		float Wavelength;
		float WaveHeight;
		D3DXVECTOR3 WindDirection;
		float WindForce;
	};

	Water(Level *pLevel, InitInfo& info);
	/*
	* Destroys the Water object.
	*/
	virtual ~Water(void);

	virtual void Initialize(ContentManager *pContentManager);
	virtual void Tick(const InputState & refInputState);
	virtual void Draw(const RenderContext* pRenderContext);
	virtual void DrawRefraction(const RenderContext* pRenderContext, D3DXPLANE refractionplane){}
	virtual void DrawReflection(const RenderContext* pRenderContext, D3DXMATRIX reflectionviewmatrix, D3DXPLANE reflectionplane){}
	virtual void DrawShadowMap(const RenderContext* pRenderContext){}
	void SetReflectionTexture(ID3D10ShaderResourceView* reflection){ m_pTextureReflection = reflection; }
	void SetRefractionTexture(ID3D10ShaderResourceView* refraction) {m_pTextureRefraction = refraction; }
	float GetHeight();

private:
	void CreateEffect(ContentManager *pContentManager);
	void GetEffectVariables(ID3D10Effect* pEffect);
	void BuildVertexBuffer();
	void BuildIndexBuffer();

	void AddTriangle (unsigned int a, unsigned int b, unsigned c);
	void AddQuad (unsigned int a, unsigned int b, unsigned c, unsigned d);
	void CreateVerticesAndTriangles();
	void DefineInputlayout();

	////////////
	// Variables
	////////////

	////////
	// Shader Variables
	////////
	ID3D10InputLayout*			m_pVertexLayout;
	ID3D10Buffer*				m_pVertexBuffer;
	ID3D10Buffer*				m_pIndexBuffer;
	ID3D10Effect*				m_pDefaultEffect;
	ID3D10EffectTechnique*		m_pDefaultTechnique;
	//Textures
	ID3D10ShaderResourceView*	m_pTextureReflection;
	ID3D10ShaderResourceView*	m_pTextureRefraction;
	ID3D10ShaderResourceView*	m_pWaterBumpMap;

	//Shader resources for textures
	ID3D10EffectShaderResourceVariable* m_pTextureReflectionVariable;
	ID3D10EffectShaderResourceVariable* m_pTextureRefractionVariable;
	ID3D10EffectShaderResourceVariable* m_pWaterBumpMapVariable;

	ID3D10EffectMatrixVariable* m_pWorldViewProjectionVariable;
	ID3D10EffectMatrixVariable* m_pWorldVariable;
	ID3D10EffectMatrixVariable* m_pReflectionWorldViewProjectionVariable;

	ID3D10EffectScalarVariable* m_pWaveLengthVariable;
	ID3D10EffectScalarVariable* m_pWaveHeightVariable;
	ID3D10EffectVectorVariable* m_pCameraPosVariable;

	ID3D10EffectScalarVariable* m_pTimeVariable;
	ID3D10EffectVectorVariable* m_pWindDirectionVariable;
	ID3D10EffectScalarVariable* m_pWindForceVariable;

	ID3D10EffectVariable* mfxLightVar;
	/////////
	// Object Variable
	/////////
	vector<VertexPosNormTex> m_VecVertices;
	vector<DWORD> m_VecIndices;

	float m_WaveLength;
	float m_WaveHeight;

	float m_WaterTime;
	D3DXVECTOR3 m_WindDirection;
	float m_WindForce;

	InitInfo m_IntInfo;
	// -------------------------
	// Disabling default copy constructor and default assignment operator.
	// If you get a linker error from one of these functions, your class is internally trying to use them. This is
	// an error in your class, these declarations are deliberately made without implementation because they should never be used.
	// -------------------------
	Water(const Water& t);
	Water& operator=(const Water& t);
};